#include "sessiondetailwidget.h"
#include <QVBoxLayout>
#include <QPushButton>
#include "choosefriendwidget.h"


sessionDetailItem::sessionDetailItem(const QIcon &icon, const QString &name)
{
    //基本属性
    this->setFixedSize(70, 80);

    //添加layout
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignHCenter); // 水平居中对齐
    this->setLayout(layout);


    //创建头像按钮
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(QSize(40, 40));
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    avatarBtn->setIconSize(QSize(40, 40));
    avatarBtn->setIcon(icon);
    avatarBtn->setStyleSheet("QPushButton {border: none} QPushButton:pressed {background-color: rgb(210, 210, 210);");

    //创建label
    nameLabel = new QLabel();
    nameLabel->setText(name);
    QFont font("微软雅黑", 10);
    nameLabel->setFont(font);

    //截断处理，name较长的情况
    int MAX_WIDTH = 60;
    QFontMetrics fm(font);
    int textWidth = fm.horizontalAdvance(name);

    if(textWidth > MAX_WIDTH)
    {
        int detail = fm.horizontalAdvance("...");
        int size = ((double)(MAX_WIDTH - (double)detail)/ textWidth) * name.size();
        QString newname = name.left(size) + "...";
        nameLabel->setText(newname);
    }

    //添加到布局管理其中

    layout->addWidget(avatarBtn);
    layout->addWidget(nameLabel, 0, Qt::AlignCenter);
}



sessionDetailWidget::sessionDetailWidget(QWidget* parent)
    :QDialog(parent)
{
    //基础信息
    this->setWindowTitle("会话详细信息");
    this->setWindowIcon(QIcon(":/resource/images/logo.png"));
    this->setFixedSize(260, 200);
    this->setAttribute(Qt::WA_DeleteOnClose);

    //设置布局管理器

    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(20, 15, 20, 0);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);


    //添加分组按钮
    sessionDetailItem* addGroupBtn = new sessionDetailItem(QIcon(":/resource/images/cross.png"), "添加");

    //添加好友按钮
    sessionDetailItem* friendInfoBtn = new sessionDetailItem(QIcon(":/resource/images/avatar2.png"), "俩啊擦擦三四次");

    //添加 "删除好友" 按钮
    QPushButton* delFriendBtn = new QPushButton();
    delFriendBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    delFriendBtn->setFixedHeight(50);
    delFriendBtn->setText("删除好友");
    QString style = R"(
    QPushButton {
        border-radius: 5px;
        border: 1px solid gray;
        background-color: rgb(230, 230, 230);
    }
    QPushButton:hover { background-color: rgb(240, 240, 240); }
    QPushButton:pressed { background-color: rgb(210, 210, 210); }
    )";

    //添加到布局管理其中
    layout->addWidget(addGroupBtn, 0, 0);
    layout->addWidget(friendInfoBtn, 0, 1);
    layout->addWidget(delFriendBtn, 1, 0, 1, 4);

    //添加一个选择好友弹出框
    QPushButton* addChooseBtn =  addGroupBtn->getAvatar();
    // addChooseBtn->setStyleSheet("QPushButton:pressed {background-color: rgb(210, 210, 210);}");
    connect(addChooseBtn, &QPushButton::clicked, this, [=](){
        chooseFriendWidget* cfw = new chooseFriendWidget(this);
        cfw->exec();
    });
}


